<template> 
   <div class="JNPF-common-layout"> 
    <div class="JNPF-common-layout-center"> 
     <el-row class="JNPF-common-search-box" :gutter="16"> 
      <el-form @submit.native.prevent="">
		<el-col :span="6">
			<el-form-item label="状态">
				<el-select v-model="query.zt" placeholder="状态" clearable >
					<el-option v-for="(item, index) in ztOptions" :key="index" :label="item.fullName" :value="item.id"  />
				</el-select>
			</el-form-item>
		</el-col>
        <!-- <el-col :span="6">
            <el-form-item label="备案时间">
                <el-date-picker
                v-model="query.basj"
                format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择日期">
                </el-date-picker> -->
              <!-- <el-date-picker v-model="pickerVal" type="daterange" start-placeholder="开始日期"
                end-placeholder="结束日期" :picker-options="pickerOptions"
                clearable :editable="false">
              </el-date-picker> -->
            <!-- </el-form-item>
        </el-col> -->
		<el-col :span="6">
			<el-form-item label="重大危险源名称" style="margin-right:50px">
				<el-input v-model="query.zdwxymc" placeholder="重大危险源名称" clearable  />
			</el-form-item>
		</el-col>

        <el-col :span="6"> 
            <el-form-item> 
                <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button> 
                <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button> 

            </el-form-item> 
       </el-col> 
      </el-form> 
     </el-row> 
     <div class="JNPF-common-layout-main JNPF-flex-main"> 
      <div class="JNPF-common-head"> 
       <div> 
		 <!-- <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">
		 新增
		 </el-button>
		 <el-button type="text" icon="el-icon-download" @click="exportData()">
		 导出
		 </el-button> -->

       </div> 
       <div class="JNPF-common-head-right"> 
        <el-tooltip effect="dark" content="刷新" placement="top"> 
         <el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false" @click="reset()" /> 
        </el-tooltip> 
        <screenfull />
       </div> 
      </div> 
      <JNPF-table v-loading="listLoading" :data="list">
		<el-table-column prop="zdwxymc" label="重大危险源名称" align="left" />
		<el-table-column label="危险源等级"  prop="wxydj" algin="left">
			<template slot-scope="scope">{{ scope.row.wxydj | dynamicText(wxydjOptions) }}</template>
		</el-table-column>
		<el-table-column label="地理位置"  prop="dz" algin="left">
			<template slot-scope="scope">{{ scope.row.dz | dynamicText(dzOptions) }}</template>
		</el-table-column>
		<el-table-column label="管辖部门"  prop="gxbm" algin="left">
			<template slot-scope="scope">{{ scope.row.gxbm | dynamicText(gxbmOptions) }}</template>
		</el-table-column>
		<el-table-column prop="basj" label="备案时间" align="left" />
		<el-table-column label="状态"  prop="zt" algin="left">
			<template slot-scope="scope">{{ scope.row.zt | dynamicText(ztOptions) }}</template>
		</el-table-column>

        <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
                <el-button type="text" v-if='scope.row.zt==1' @click="addOrUpdateHandle(scope.row.id)" >
                备案
                </el-button>
               <el-button type="text" v-if='scope.row.zt==2 || scope.row.zt==3' @click="addOrUpdateHandle(scope.row.id)" >
                变更
                </el-button>
                <el-button type="text" v-if='scope.row.zt==2 || scope.row.zt==3' @click="addOrHeXiao(scope.row.id)">
                核销
                </el-button>
                <el-button type="text" v-if='scope.row.zt==2 || scope.row.zt==3 || scope.row.zt==4' @click="addOrUpdateHandle(scope.row.id,true,scope.row.zt)" >
                查看
                </el-button>
            </template>
        </el-table-column>
       
      </JNPF-table> 
      <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />

     </div> 
    </div> 
    <JNPFForm v-if="formVisible" ref="JNPFForm" @refresh="refresh" /> 
    <FormHX v-if="formHXVisible" ref="JNPFFormHX" @refresh="refresh" /> 
    <exportbox v-if="exportBoxVisible" ref="ExportBox" @download="download" /> 
   </div> 
</template> 
<script>
    import request from '@/utils/request'
    import {getDictionaryDataSelector} from '@/api/systemData/dictionary'
    import JNPFForm from './Form'
    import FormHX from './HX'
    import ExportBox from './ExportBox'
    import {previewDataInterface} from '@/api/systemData/dataInterface'

    export default {
        components: {JNPFForm, ExportBox, FormHX},
        data() {
            return {
                showAll: false,
                query: {
					zt:undefined,
					basj:undefined,
					zdwxymc:undefined,

                },
                defaultProps: {
                    children: 'children',
                    label: 'fullName'
                },
                list: [],
                listLoading: true,
                total: 0,
                listQuery: {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                },
                formVisible: false,
                formHXVisible: false,  //-
                exportBoxVisible: false,
                columnList: [
					{prop: 'zdwxymc', label: '重大危险源名称'},
					{prop: 'wxydj', label: '危险源等级'},
					{prop: 'dz', label: '地理位置'},
					{prop: 'gxbm', label: '管辖部门'},
					{prop: 'basj', label: '备案时间'},
					{prop: 'zt', label: '状态'},

                ],
				dzOptions:[],
				gxbmOptions:[],
				wxydjOptions:[{"fullName":"一级","id":"01"},{"fullName":"二级","id":"02"},{"fullName":"三级","id":"03"},{"fullName":"四级","id":"04"},{"fullName":"不构成危化品重大危险源","id":"05"}],
				ztOptions:[{"fullName":"未备案","id":"1"},{"fullName":"已备案","id":"2"},{"fullName":"已更改","id":"3"},{"fullName":"已核销","id":"4"}],
                pickerVal: [],
                // pickerOptions: {
                //     shortcuts: [{
                //     text: '最近一周',
                //     onClick(picker) {
                //         const end = new Date();
                //         const start = new Date();
                //         start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                //         picker.$emit('pick', [start, end]);
                //     }
                //     }, {
                //     text: '最近一个月',
                //     onClick(picker) {
                //         const end = new Date();
                //         const start = new Date();
                //         start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                //         picker.$emit('pick', [start, end]);
                //     }
                //     }, {
                //     text: '最近三个月',
                //     onClick(picker) {
                //         const end = new Date();
                //         const start = new Date();
                //         start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                //         picker.$emit('pick', [start, end]);
                //     }
                //     }]
                // },
                startTime: '',
                endTime: '',
            }
        },
        computed: {},
        created() {
            this.initData();
            this.getDzOptions();
            this.getGxbmOptions();
        },
        methods: {
            getDzOptions()
            {
                previewDataInterface('e7b2c31f5a7147c08629c5409dd7fdf4').then(res => {
                    this.dzOptions = res.data
                })
            },
            getGxbmOptions()
            {
                previewDataInterface('772d2cbcadd24df79243e3d92ef5f690').then(res => {
                    this.gxbmOptions = res.data
                })
            },

            initData() {
                this.listLoading = true;
                let query = {
                    ...this.listQuery,
                    ...this.query,
                    // startTime: this.startTime,
                    // endTime: this.endTime,
                };
                request({
                    url: `/api/System/ZDWXYDA`,
                    method: 'get',
                    data: query
                }).then(res => {
                    this.list = res.data.list
                    this.total = res.data.pagination.total
                    this.listLoading = false
                    this.$nextTick(() => {
                        this.tableHeight = "100%"
                        this.zt = this.query.zt;
                    });
                })
            },
            handleDel(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    request({
                        url: `/api/System/ZDWXYDA/${id}`,
                        method: 'DELETE'
                    }).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.msg,
                            onClose: () => {
                                this.initData()
                            }
                        });
                    })
                }).catch(() => {
                });
            },
            addOrUpdateHandle(id,isDetail,zt) {
                if(zt == 4){
                    this.formHXVisible = true
                    this.$nextTick(() => {
                        this.$refs.JNPFFormHX.init(id,isDetail)
                    })
                }else{
                    this.formVisible = true
                    this.$nextTick(() => {
                        this.$refs.JNPFForm.init(id,isDetail)
                    })
                }
            },
            //-
            addOrHeXiao(id,isDetail){
                this.formHXVisible = true
                this.$nextTick(() => {
                    this.$refs.JNPFFormHX.init(id,isDetail)
                })
            },
            exportData() {
                this.exportBoxVisible = true
                this.$nextTick(() => {
                    this.$refs.ExportBox.init(this.columnList)
                })
            },
            download(data) {
                let query = {...data, ...this.listQuery, ...this.query}
                request({
                    url: `/api/System/ZDWXYDA/Actions/Export`,
                    method: 'POST',
                    data: query
                }).then(res => {
                    if (!res.data.url) return
                    window.location.href = this.define.comUrl + res.data.url
                    this.$refs.ExportBox.visible = false
                    this.exportBoxVisible = false
                })
            },
            search() {
                if (this.pickerVal && this.pickerVal.length) {
                    this.startTime = this.pickerVal[0]
                    this.endTime = this.pickerVal[1]
                } else {
                    this.startTime = ''
                    this.endTime = ''
                } 
                this.listQuery = {
					currentPage: 1,
					pageSize: 20,
                    sort: "desc",
                    sidx: "",
                }
                this.initData()
            },
            refresh(isrRefresh) {
                this.formVisible = false
                this.formHXVisible = false
                if (isrRefresh) this.reset()
            },
            reset() {
                for (let key in this.query) {
                    this.query[key] = undefined
                }
                this.listQuery = {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                }
                this.pickerVal = []
                this.initData()
            }
        }
    }
</script>